package com.weiiew.itemservice.service;

import com.example.feignapi.client.ImageClient;
import com.weiiew.common.entity.cal.calItem;
import com.weiiew.common.entity.factory.FactoryRecipe;
import com.weiiew.common.entity.simple.Pair;
import com.weiiew.common.entity.simple.SimpleItem;
import com.weiiew.common.entity.dto.Item;
import com.weiiew.common.entity.qo.ItemQo;
import com.weiiew.common.entity.vo.ItemVO;
import com.weiiew.itemservice.annotation.ItemChangeCacheEvict;
import com.weiiew.itemservice.mapper.ItemMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 魏锴
 * @description 针对表【item】的数据库操作Service实现
 * @createDate 2024-02-05 00:34:27
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ItemServiceImpl implements ItemService {
    private final ItemMapper baseMapper;
    private final ImageClient imageClient;
    @Override
    @ItemChangeCacheEvict
    public boolean insertItem(Item item, int id, int version) {
        int insert = baseMapper.insert(item);
        int icon = baseMapper.setIcon(item.getId());
        return insert == 1 && icon == 1;
    }

    @Override
    @ItemChangeCacheEvict
    public boolean updateItem(Item item, int id, int version) {
        return baseMapper.updateById(item) == 1;
    }

    @Override
    @ItemChangeCacheEvict
    public boolean removeItem(int id, int version) {
        int i = baseMapper.deleteById(id);
        imageClient.removeImage("items", String.valueOf(version), baseMapper.getIconById(id));
        return i == 1;
    }

    @Override
    @Cacheable(cacheNames = "item:one", key = "#id")
    public Item getItemById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Integer getItemByName(String name, Integer version) {
        return baseMapper.getItemByName(name, version);
    }


    @Override
    @Cacheable(cacheNames = "item:list:sizes", key = "#version")
    public List<Integer> getItemStackSizes(Integer version) {
        return baseMapper.getItemStackSizes(version);
    }

    @Override
    @Cacheable(cacheNames = "item:list:simple", key = "#qo.noPageKey")
    public List<SimpleItem> getSimpleItemList(ItemQo qo) {
        return baseMapper.getSimpleItemList(qo);
    }
    @Override
    @Cacheable(cacheNames = "item:list", key = "#qo.hasPageKey")
    public List<ItemVO> getItemList(ItemQo qo) {
        return baseMapper.getItemList(qo);
    }

    @Override
    @Cacheable(cacheNames = "item:length", key = "#qo.noPageKey")
    public Integer getItemLength(ItemQo qo) {
        return baseMapper.getItemLength(qo);
    }

    @Override
    @Cacheable(cacheNames = "item:vo", key = "#id")
    public ItemVO getItemVOById(Integer id) {
        return baseMapper.getItemById(id);
    }

    @Override
    @Cacheable(cacheNames = "item:list:use", key = "#version")
    public List<SimpleItem> getItemListOrderByUse(Integer version) {
        return baseMapper.getItemListOrderByUse(version);
    }

    @Override
    @Cacheable(cacheNames = "item:icons", key = "#version")
    public List<String> getItemIconList(Integer version) {
        return baseMapper.getItemIconList(version);
    }

    @Override
    @Cacheable(cacheNames = "cal:recipe:item", key = "#version + '&' + #name")
    public List<calItem> getCalRecipeItemList(Integer version, String name) {
        return baseMapper.getCalRecipeItemList(version, name);
    }
    @Override
    @Cacheable(cacheNames = "recipe:item:recipe:map", key = "#version", cacheManager = "mapCacheManager" )
    public Map<Integer, Integer> getItemRecipeMap(int version) {
        List<FactoryRecipe> recipes = baseMapper.getRecipeList(version);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (FactoryRecipe r : recipes) map.put(r.getItem(), r.getRecipe());
        return map;
    }


    @Cacheable(cacheNames = "item:id:name:map", key = "#version", cacheManager = "mapCacheManager")
    public Map<Integer, String> getIdNameMap(int version) {
        List<Pair> pairs = baseMapper.getIdNameList(version);
        Map<Integer, String> map = new HashMap<>();
        pairs.forEach(pair -> map.put(pair.getId(), pair.getName()));
        return map;
    }


    @Override
    @CacheEvict(cacheNames = "item:list:simple", key = "#version + '*'", cacheManager = "fuzzyCacheManager")
    public void clearCache(int version) {
        System.out.println("执行函数");
    }
}




